package 牛客.链表;

import java.util.Scanner;

class Node2{
    Object data;
    Node2 next;

    public Node2(Object data, Node2 next) {
        this.data = data;
        this.next = next;
    }
}

public class Link {
    Node2 head;
    int size;

    //初始化
    public void Initiate(){
        head = new Node2(null,null);
        size = 0;
    }
    //链表大小
    public int Length(){
        return size;
    }

    //取第i个位置的元素
    public Node2 getNode(int i){
        if(i <= size){
            Node2 p;
            p = head;
            for (int j = 0; j <= i; j++) {
                p = p.next;
            }
            return p;
        }
        else{
            System.out.println("i索引不合法！");
            return null;
        }
    }

    //在第i个位置插入元素
    public void Insert(int i, Object obj){
        Node2 p = getNode(i - 1);
        if(p != null){
            Node2 q = new Node2(obj,p);
            q.next = p.next;
            p.next = q;
            size++;
        }
    }

    //删除第i个元素
    public void Delete(int i){
        Node2 p = getNode(i - 1);
        if(p != null){
            p.next = p.next.next;
            size--;
        }
    }

    //链表逆置 （带头结点）
    public void reset(){
        Node2 p,q;
        p = head.next;
        head.next = null;
        while(p != null){
            q = p;
            p = p.next;
            q.next = head.next;
            head.next = q;
        }
    }

    //链表逆置 （不带头结点）
    /*
    public ListNode ReverseList(ListNode head){
        ListNode Pre = null;  //始终保存前面断掉的链表的头指针
        ListNode Cur = head;   //当前处理的节点
        ListNode pReserve = null; //保存当前节点的下一个节点，防止断掉了
        while ( Cur != null)
        {
            pReserve = Cur.next;
            Cur.next = Pre;
            Pre = Cur;
            Cur = pReserve;
        }
        return Pre;
    }
     */
    //判断链表是否有环
    public boolean hasCycle(Node2 head) {
        Node2 fast = head,slow = head;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast)
                return true;
        }
        return false;
    }
    public static void main(String[] args) {
        Link list = new Link();
        list.Initiate();
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] strArray = str.split(",");
        for (int i = 0; i < strArray.length; i++) {
            list.Insert(i, strArray[i]);
        }
        System.out.println(list.hasCycle(list.head));
        list.reset();
//        list.Delete(2);
        for (int i = 0; i < list.size; i++) {
            if(i < list.size - 1)
                System.out.print(list.getNode(i).data + ",");
            else
                System.out.print(list.getNode(i).data);
        }

    }
}
